import {DataRouter} from "../../../script/frame/dataSource/DataRouter";
import {GuiKeys} from "../../../script/controller/model/ControllerOpt";
import Configer from "../../../script/frame/apiModel/config/Configer";
import {UIConfigKey} from "../../../script/constant/UIConfig";
import {ItemJson} from "../../../script/frame/apiModel/model/StoreJson";
import * as React from "../../../script/frame/apiModel/gui/React";
import {CodeUtil} from "../../../script/frame/codeAdapter/CodeUtil";
import {ItemType} from "../../../script/frame/dataSource/DataModel";
import {ResourceKey} from "../../../script/constant/ResourceConstant";
import {View} from "../../../script/frame/apiModel/gui/React";

interface IProp {
    x: number,
    y: number,
    goodsInfo: ItemJson,
    hidden: boolean,
}
interface IState {
}

export default class GoodsInfoPage extends React.Element<IProp, IState> {

    render = () => {
        const yMargin = 0.00332;
        const backSize = { h: yMargin, w: 0.25000 }
        const backPos = { x: this.props.x, y: this.props.y }
        const xPadding = 0.00500;
        const goodsId = this.props.goodsInfo?.id;
        const fontSize = Configer.getUIConfig(UIConfigKey.fontSize);
        let retStr = '';
        const retPos = { x: 0, y: 0 }
        const retSize = { h: 0, w: 0 }
        if (goodsId) {
            const itemData = DataRouter.getItemDataByItemId(goodsId);
            if (itemData) {
                if (itemData.item_type === ItemType.equip) {
                    const name = itemData.Name;
                    if (name) {
                        retStr = retStr + `${name}\n`;
                    }
                    // let levelStr;
                    // if (goodsData.good_level) {
                    //     const hero = GlobalEnv.getHero(player);
                    //     if (hero && GetHeroLevel(hero) >= goodsData.good_level) {
                    //         levelStr = `|cff20c000穿戴等级: ${goodsData.good_level}|r`;
                    //     } else {
                    //         levelStr = `|cffff0303穿戴等级: ${goodsData.good_level}|r`;
                    //     }
                    //     retStr = retStr + `${levelStr}\n`;
                    // }
                    // const positionStr = Util.getNameByPosition(goodsData.position);
                    // retStr = retStr + `${positionStr}\n`;
    
                    if (itemData.Ubertip) {
                        const descriptLineStr = itemData.Ubertip;
                        retStr = retStr + `${descriptLineStr}\n`;
                    }
    
                    const affixStrs = this.props.goodsInfo?.af;
                    if (affixStrs) {
                        let retAffixStr = '';
                        const affixArr = CodeUtil.getArrBySplit(affixStrs);
                        for (let i = 0; i < affixArr?.length; i++) {
                            const affixId = affixArr[i];
                            const affixData = DataRouter.getAffixDatasByAffixID(affixId);
                            if (affixData) {
                                retAffixStr = `${retAffixStr}${affixData.Ubertip}\n`;
                            }
                        }
                        retStr = retStr + `${retAffixStr}`;
                    }
                } else if (itemData) {
                    const name = itemData.Name;
                    if (name) {
                        retStr = retStr + `${name}\n`;
                    }
    
                    if (itemData.Ubertip) {
                        const descriptLineStr = itemData.Ubertip;
                        retStr = retStr + `${descriptLineStr}\n`;
                    }
                }
    
                const retLineArr = retStr.split('\n');
                const retLineNum = retLineArr.length + 2;
    
                retSize.h = retLineNum * fontSize;
                retSize.w = backSize.w - xPadding * 2;
    
                backSize.h = backSize.h + retSize.h + yMargin * 2;
            }
        }

        const goodInfoPagePath = Configer.getResource(ResourceKey.infoPagePath);

        return <View   key={GuiKeys.goodInfoPage} style={{ hidden: this.props.hidden, backgroundImage: goodInfoPagePath, left: backPos.x, top: backPos.y, width: backSize.w, height: backSize.h }}>
            <View   key={GuiKeys.goodInfoPageText} style={{ marginL: retPos.x, marginT: retPos.y, width: retSize.w, height: retSize.h }}>{retStr}</View>
        </View>
    }
        // const yMargin = 0.002;
        // const backSize = { h: yMargin, w: 0.2 }
        // const backPos = { x, y }
        // const childProps: GuiProps[] = []
        // const xPadding = 0.004;
        // const goodsId = goodsInfo?.id;
        // const fontSize = Configer.getUIConfig(UIConfigKey.fontSize);
        // if (goodsId) {
        //     let retStr = '';
        //     const goodsData = DataRouter.getGoodDataByGoodId(goodsId);
        //     const itemData = DataRouter.getItemDataByItemId(goodsId);
        //     if (goodsData) {
        //         const name = goodsData.Name;
        //         if (name) {
        //             retStr = retStr + `${name}\n`;
        //         }
        //         // let levelStr;
        //         // if (goodsData.good_level) {
        //         //     const hero = GlobalEnv.getHero(player);
        //         //     if (hero && GetHeroLevel(hero) >= goodsData.good_level) {
        //         //         levelStr = `|cff20c000穿戴等级: ${goodsData.good_level}|r`;
        //         //     } else {
        //         //         levelStr = `|cffff0303穿戴等级: ${goodsData.good_level}|r`;
        //         //     }
        //         //     retStr = retStr + `${levelStr}\n`;
        //         // }
        //         // const positionStr = Util.getNameByPosition(goodsData.position);
        //         // retStr = retStr + `${positionStr}\n`;
        //
        //         if (goodsData.Ubertip) {
        //             const descriptLineStr = goodsData.Ubertip;
        //             retStr = retStr + `${descriptLineStr}\n`;
        //         }
        //
        //         const affixStrs = goodsInfo.af;
        //         if (affixStrs) {
        //             let retAffixStr = '';
        //             const affixArr = CodeUtil.getArrBySplit(affixStrs);
        //             for (let i = 0; i < affixArr?.length; i++) {
        //                 const affixId = affixArr[i];
        //                 const affixData = DataRouter.getAffixDatasByAffixID(affixId);
        //                 if (affixData) {
        //                     retAffixStr = `${retAffixStr}${affixData.Ubertip}\n`;
        //                 }
        //             }
        //             retStr = retStr + `${retAffixStr}`;
        //         }
        //     } else if (itemData) {
        //         const name = itemData.Name;
        //         if (name) {
        //             retStr = retStr + `${name}\n`;
        //         }
        //
        //         if (itemData.Ubertip) {
        //             const descriptLineStr = itemData.Ubertip;
        //             retStr = retStr + `${descriptLineStr}\n`;
        //         }
        //     }
        //
        //     const retLineArr = retStr.split('\n');
        //     const retLineNum = retLineArr.length - 1;
        //     const retSize = { h: retLineNum * fontSize, w: backSize.w - xPadding * 2 }
        //     const retPos = { x: xPadding, y: 0 - backSize.h }
        //     backSize.h = backSize.h + retSize.h + yMargin * 2;
        //     backPos.x = backPos.x - backSize.w;
        //     backPos.y = backPos.y + backSize.h;
        //     const retProp: GuiProps = {
        //         text: retStr,
        //         childNum: 0,
        //         childProps: [],
        //         id: this.getName(),
        //         position: retPos,
        //         size: retSize,
        //     };
        //     childProps.push(retProp);
        // }
        // const goodInfoPagePath = Configer.getUIConfig(UIConfigKey.infoPagePath);
        // const backProp: GuiProps = {
        //     backgroundImage: goodInfoPagePath,
        //     childProps,
        //     childNum: 1,
        //     id: this.getName(),
        //     position: backPos,
        //     size: { w: backSize.w, h: backSize.h },
        // };
        // this.toRender(player, backProp, DzGetGameUI());
}